package com.imy.lua.lib.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.immomo.mls.LuaViewManager;
import com.immomo.mls.annotation.LuaBridge;
import com.immomo.mls.annotation.LuaClass;
import com.immomo.mls.utils.LVCallback;
import com.immomo.mls.wrapper.callback.IVoidCallback;
import com.imy.common.util.ALog;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;

/**
 * Created by MLN Template 注册方法： Register.newSHolderWithLuaClass(UIHandler.LUA_CLASS_NAME, UIHandler.class)
 */
@LuaClass(isStatic = true)
public class UIHandler {
  private static Handler mHandler = new Handler(Looper.getMainLooper());
  /**
   * Lua类名
   */
  public static final String LUA_CLASS_NAME = "Ui";

  @LuaBridge
  public static void run(LVCallback lvCallback) {
    mHandler.post(new Runnable() {
      @Override
      public void run() {
          ALog.i("执行在UI线程");
          lvCallback.call();
      }
    });
  }

  /**
   * 获取上下文，一般情况，此上下文为Activity
   *
   * @param globals 虚拟机，可通过构造函数存储
   */
  protected static Context getContext(@NonNull Globals globals) {
    LuaViewManager m = (LuaViewManager) globals.getJavaUserdata();
    return m != null ? m.context : null;
  }
}
